package com.battcn.platform.controller.crawler;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.battcn.platform.entity.DataGrid;
import com.battcn.platform.entity.crow.CommentWord;
import com.battcn.platform.entity.crow.Post;
import com.battcn.platform.entity.crow.Statistics;
import com.battcn.platform.entity.crow.TitleWord;
import com.battcn.platform.entity.crow.User;
import com.battcn.platform.entity.crow.AuxiliaryModels.NameValue;
import com.battcn.platform.entity.crow.movie.Actor;
import com.battcn.platform.entity.crow.movie.Director;
import com.battcn.platform.entity.crow.movie.Movie;
import com.battcn.platform.service.dataAnalysis.CommentService;
import com.battcn.platform.service.dataAnalysis.CommentWordService;
import com.battcn.platform.service.dataAnalysis.MovieService;
import com.battcn.platform.service.dataAnalysis.PostService;
import com.battcn.platform.service.dataAnalysis.TitleWordService;
import com.battcn.platform.service.dataAnalysis.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;

/**
 * 展示图表
 * Created by yanglei on 17/10/21.
 */
@Controller
@RequestMapping("/dataAnalysis")
@Api(tags = "1.5", description = "图表展示")
public class DataAnalysisController {
   
    @Autowired
    private MovieService movieService;
   
    /*
    @RequestMapping(value = "/genCommentWord")
    public void genCommentWord() {
        commentWordService.insertAllWords();
    }
    */
    @RequestMapping(value = "/movie/list")
	public String list() throws Exception
	{
		return "crawler/movie/List";
	}
	@RequestMapping(value = "/actor/list")
	public String actorList() throws Exception
	{
		return "crawler/movie/actorList";
	}
	
	@RequestMapping(value = "/director/list")
	public String directList() throws Exception
	{
		return "crawler/movie/directList";
	}
	/**
	 * 展示电影图表
	 * 1为演员id
	 * 2为导演id
	 * */
	@RequestMapping(value = "/showMovieData")
	public String showMovieData(Model model,Integer id,Integer type)
	{	
		model.addAttribute("id",id);
		model.addAttribute("type",type);
		return "crawler/movie/showMovieData";
	}
	/**
	 * 获取电影图表数据
	 * 1为演员id
	 * 2为导演id
	 * */
	@RequestMapping(value = "/getMovieData",method=RequestMethod.POST)
	@ResponseBody
	public Statistics getMovieData(Model model,Integer id,Integer type)
	{	
		Statistics statistics=new Statistics();
		String movieIds="";
		if(type==1){
			movieIds=movieService.selectByActorId(id).getMovieids();
		}
		if(type==2){
			movieIds=movieService.selectByDirectorId(id).getMovieids();
		}
		statistics=movieService.getMovieStatics(movieIds);
		return statistics;
	}
	
	@RequestMapping(value = "/actor/query")
	@ResponseBody
	public PageInfo<Actor> queryActor(Model model,DataGrid grid,@RequestParam(value="name",defaultValue="")String name) throws Exception
	{
		PageHelper.startPage(grid.getPageNum(), grid.getPageSize());
		PageHelper.orderBy(grid.getSort());
		List<Actor> list=movieService.getActorList(name);
		return new PageInfo<Actor>(list);
	}
	
	@RequestMapping(value = "/movie/query")
	@ResponseBody
	public PageInfo<Movie> queryMovie(Model model,DataGrid grid,@RequestParam(value="name",defaultValue="")String name) throws Exception
	{
		PageHelper.startPage(grid.getPageNum(), grid.getPageSize());
		PageHelper.orderBy(grid.getSort()+" "+grid.getOrder());
		List<Movie> list=movieService.getMovieByName(name);
		return new PageInfo<Movie>(list);
	}
	
	/**
	 * 电影年份统计数据*/
	@RequestMapping(value = "/movie/selectMovieAnnee")
	@ResponseBody
	public List<JSONObject> selectMovieAnnee(Model model) throws Exception
	{
		return movieService.selectMovieAnnee();
	}
	
	@RequestMapping(value = "/movie/showMovieAnnee")
	public String showMovieAnnee(Model model) throws Exception
	{
		return "crawler/movie/showMovieAnnee";
	}
	/**展示电影详情数据分析*/
	@RequestMapping(value = "/movie/showMovieDetail")
	public String showMovieDetail(Model model,Integer id) throws Exception
	{
		model.addAttribute("movieDetail",movieService.selectMovieById(id));
		model.addAttribute("actorList",movieService.selectActorById(id));
		model.addAttribute("directorList",movieService.selectDirectorById(id));
		return "crawler/movie/showMovieDetail";
	}
	@RequestMapping(value = "/director/query")
	@ResponseBody
	public PageInfo<Director> queryDirector(Model model,DataGrid grid,@RequestParam(value="name",defaultValue="")String name) throws Exception
	{
		PageHelper.startPage(grid.getPageNum(), grid.getPageSize());
		PageHelper.orderBy(grid.getSort());
		List<Director> list=movieService.getDirectorList(name);
		return new PageInfo<Director>(list);
	}
	
    @RequestMapping(value = "/province")
    public String getAddressEcharts() {
        return "crawler/ProvinceAddress";
    }
   
    @RequestMapping(value = "/gender")
    public String getGenderEcharts() {
        return "crawler/Gender";
    }

    @RequestMapping(value = "/views")
    public String getViewsEcharts() {
        return "crawler/Views";
    }

    @RequestMapping(value = "/titleword")
    public String getTitleWordEcharts() {
        return "crawler/TitleWords";
    }

    @RequestMapping(value = "/title")
    public String getTitleEcharts() {
        return "crawler/Titles";
    }

    @RequestMapping(value = "/commentword")
    public String getCommentWordEcharts() {
        return "crawler/CommentWords";
    }
}
